package com.glasses.orm.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ashin on 2018/7/22.
 */
public class SensitiveWordInit {

    /**
     * 电话号码过滤
     *
     * @param phone
     * @return
     */
    public static String sensitivePhone(String phone) {
        String regex = "((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phone);
        boolean isMatch = m.find();
        if (isMatch) {
            String _phone = m.group();
            StringBuilder _stringBuffer = new StringBuilder();
            for (int i = 0; i < _phone.length(); i++) {
                _stringBuffer.append("*");
            }
            phone = phone.replace(_phone,_stringBuffer.toString());

        }
        return phone;
    }

    /**
     * QQ号过滤
     *
     * @param str
     * @return
     */
    public static String sensitiveQQ(String str) {
        return str.replaceAll("([1-9][0-9]{4,15})", "*****");
    }

    public static String sensitiveWord(String str) {
        if(str == null)
            return "";
        str = sensitivePhone(str);
        return sensitiveQQ(str);
    }

    public static void main(String[] args) {
        String str = "我的电话号码是:18652868751，QQ号是：1928374";
        System.err.println(str);
        System.err.println(sensitiveWord(str));
    }
}
